package com.han.hotelplat.service.datacenter.total.impl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Hibernate;
import org.hibernate.type.NullableType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.han.hotelplat.comm.base.EntityManage;
import com.han.hotelplat.comm.utils.DateUtil;
import com.han.hotelplat.service.datacenter.employee.EmployeeDataService;
import com.han.hotelplat.service.datacenter.finance.FinanceService;
import com.han.hotelplat.service.datacenter.hotel.HotelDataService;
import com.han.hotelplat.service.datacenter.total.AllDataTotalService;

@Service
public class AllDataTotalServiceImpl implements AllDataTotalService{
	@Autowired
	private  EntityManage entityManage;
	@Autowired
	private EmployeeDataService employeeDataService;
	@Autowired
	private HotelDataService hotelDataService;
	@Autowired
	private FinanceService financeService;
	private DecimalFormat   moneyfnum  =   new  DecimalFormat("##0.00");  
	private DecimalFormat   roomfnum  =   new  DecimalFormat("##0.0");  
	@Override
	public Map<String, Object> getAllDataTotal() {
		int emCount = 0;//今日新增帮工
		int hotelCount = 0;//今日新增酒店
		double roomCount = 0f;//今日提交房量
		double costMoney = 0f;//今日酒店支出
		//查询今日提交房量
		String sql1 = "SELECT SUM(roomcount) AS roomcount FROM tb_child_task WHERE substr(creattime,1,10 ) = substr(NOW(),1,10 ) AND result IN (2,3,4)";
		//查询今日新增帮工
		String sql2 = "SELECT COUNT(id) AS count FROM tb_employee WHERE substr(creattime,1,10 ) = substr(NOW(),1,10 )";
		//查询今日新增酒店
		String sql3 = "SELECT COUNT(id) AS count FROM tb_hotel WHERE substr(creattime,1,10 ) = substr(NOW(),1,10 )";
		//查询今日酒店支出
		String sql4 = "SELECT SUM(amount) AS money FROM tb_bill AS b, tb_wallet AS w WHERE w.id=b.expwalletid AND w.usertype=1 AND substr(b.createtime,1,10 ) = substr(NOW(),1,10 )";
	
		Map<String, Object> data = new HashMap<String, Object>();
		
		List<Object> ctli = entityManage.findBySql(sql1.toString());
		Map<String, NullableType> fieldMappingType = new HashMap<String, NullableType>();
		fieldMappingType.put("roomcount", Hibernate.DOUBLE);
		for(int i= 0 ; i<ctli.size(); i++){
			roomCount= (double) (ctli.get(i) == null ? 0d:ctli.get(i));
		}
		fieldMappingType.clear();
		
		ctli = entityManage.findBySql(sql4.toString());
		fieldMappingType.put("money", Hibernate.DOUBLE);
		for(int i= 0 ; i<ctli.size(); i++){
			if(ctli.get(i) == null){
				costMoney = 0;
			}else{
				costMoney= (double) ctli.get(i);
			}
		}
		fieldMappingType.clear();
		
		fieldMappingType.put("count", Hibernate.INTEGER);
		ctli = entityManage.findBySql(sql2.toString(),fieldMappingType);
		for(int i= 0 ; i<ctli.size(); i++){
			emCount= (Integer) ctli.get(i);
		}
		
		ctli = entityManage.findBySql(sql3.toString(),fieldMappingType);
		for(int i= 0 ; i<ctli.size(); i++){
			hotelCount = (Integer) ctli.get(i);
		}
		data.put("emCount", emCount);
		data.put("hotelCount", hotelCount);
		data.put("roomCount", roomfnum.format(roomCount));
		data.put("costMoney", moneyfnum.format(costMoney));
		return data;
	}
	
	@Override
	public List<Map<String, Object>> getSevenDayDataTotal(int type) {
		List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
		Date startDate = new Date();
		startDate = DateUtil.addDay(new Date(), -6);
		String startTime = DateUtil.formatYMD(startDate);
		String endTime = DateUtil.formatYMD(new Date());
		switch (type) {
		case 0:
			data = getEmSevenData(startTime,endTime);
			break;
		case 1:
			data = getHotelSevenData(startTime,endTime);
			break;
		case 2:
		case 3:
			data = getRoomSevenData(startTime,endTime,type);
			break;
		default:
			break;
		}
		return data;
	}

	private List<Map<String, Object>> getRoomSevenData(String startTime, String endTime, int type) {
		List<Map<String, Object>>  returnData = new ArrayList<Map<String,Object>>();
		Map<String, Object>  data = financeService.getFinanceHotelCostGroupDateResult(startTime, endTime, null, null);
		JSONArray array = (JSONArray) data.get("hotelCostData");
		Map<String, Object> totalData = new HashMap<String, Object>();
		for(Object o : array){
			JSONObject js = JSONObject.parseObject(o.toString());
			String date = js.getString("date");
			String advanceCostMoney = js.getString("realCostMoney");
			String advanceRoomCount = js.getString("advanceRoomCount");
			if(type == 2){
				totalData.put(date, advanceRoomCount);
			}else if(type == 3){
				totalData.put(date, advanceCostMoney);
			}
		}
		//排序
		List<Date> allDate = DateUtil.getBetweenDates(DateUtil.parseYMD(startTime), DateUtil.parseYMD(endTime));
		for(Date date : allDate){
			String count = (String) totalData.get(DateUtil.formatYMD(date));
			if(count == null){
				Map<String, Object> sub = new HashMap<String, Object>();
				sub.put("date", DateUtil.formatYMD(date));
				sub.put("count", 0);
				if(type == 2){
					sub.put("roomCount", count);
				}else if(type == 3){
					sub.put("costMoney", count);
				}
				returnData.add(sub);
			}else{
				Map<String, Object> sub = new HashMap<String, Object>();
				sub.put("date", DateUtil.formatYMD(date));
				if(type == 2){
					sub.put("roomCount", count);
				}else if(type == 3){
					sub.put("costMoney", count);
				}
				returnData.add(sub);
			}
		}
		return returnData;
	}

	private List<Map<String, Object>> getHotelSevenData(String startTime, String endTime) {
		List<Map<String, Object>> returnData  = new ArrayList<Map<String,Object>>();
		Map<String, Integer> totalData = hotelDataService.getNewHotelDataGroupDate(startTime, endTime, null);
		//排序
		List<Date> allDate = DateUtil.getBetweenDates(DateUtil.parseYMD(startTime), DateUtil.parseYMD(endTime));
		for(Date date : allDate){
			Integer count = (Integer) totalData.get(DateUtil.formatYMD(date));
			if(count == null){
				Map<String, Object> sub = new HashMap<String, Object>();
				sub.put("date", DateUtil.formatYMD(date));
				sub.put("count", 0);
				returnData.add(sub);
			}else{
				Map<String, Object> sub = new HashMap<String, Object>();
				sub.put("date", DateUtil.formatYMD(date));
				sub.put("count", count);
				returnData.add(sub);
			}
		}
		return returnData;
	}

	private List<Map<String, Object>> getEmSevenData(String startTime, String endTime) {
		List<Map<String, Object>> returnData  = new ArrayList<Map<String,Object>>();
		Map<String, Integer> totalData = employeeDataService.getAddEmployeeGroupByDate(startTime, endTime);
		//排序
		List<Date> allDate = DateUtil.getBetweenDates(DateUtil.parseYMD(startTime), DateUtil.parseYMD(endTime));
		for(Date date : allDate){
			Integer count = (Integer) totalData.get(DateUtil.formatYMD(date));
			if(count == null){
				Map<String, Object> sub = new HashMap<String, Object>();
				sub.put("date", DateUtil.formatYMD(date));
				sub.put("count", 0);
				returnData.add(sub);
			}else{
				Map<String, Object> sub = new HashMap<String, Object>();
				sub.put("date", DateUtil.formatYMD(date));
				sub.put("count", count);
				returnData.add(sub);
			}
		}
		return returnData;
	}
}
